home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl -w
- # This file was preprocessed, do not edit!
-
-
- use strict;
- use POSIX;
- use Fcntl;
- use Getopt::Long;
- use Debconf::Client::ConfModule ();
-
- my ($config, $start, $from, $to, $stop);
- my ($logfile, $logstderr);
-
- sub checkopen (@) {
- my $file = $_[0];
- my $fd = POSIX::open($file, &POSIX::O_RDONLY);
- defined $fd or die "$0: can't open $_[0]: $!\n";
- return $fd;
- }
-
- sub checkclose ($) {
- my $fd = $_[0];
- unless (POSIX::close($fd)) {
- return if $! == &POSIX::EBADF;
- die "$0: can't close fd $fd: $!\n";
- }
- }
-
- sub checkdup2 ($$) {
- my ($oldfd, $newfd) = @_;
- checkclose($newfd);
- POSIX::dup2($oldfd, $newfd)
- or die "$0: can't dup fd $oldfd to $newfd: $!\n";
- }
-
- sub nocloexec (*) {
- my $fh = shift;
- my $flags = fcntl($fh, F_GETFD, 0);
- fcntl($fh, F_SETFD, $flags & ~FD_CLOEXEC);
- }
-
- sub reservefds (@) {
- my $null = checkopen('/dev/null');
- my $close = 1;
- for my $fd (@_) {
- if ($null == $fd) {
- $close = 0;
- } else {
- checkclose($fd);
- checkdup2($null, $fd);
- }
- }
- if ($close) {
- checkclose($null);
- }
- }
-
- sub envnonempty ($) {
- my $name = shift;
- return (exists $ENV{$name} and $ENV{$name} ne '');
- }
-
- sub start_debconf (@) {
- if (! $ENV{DEBIAN_HAS_FRONTEND}) {
- if (envnonempty('DEBCONF_DB_REPLACE')) {
- $ENV{DEBCONF_APT_PROGRESS_DB_REPLACE} =
- $ENV{DEBCONF_DB_REPLACE};
- }
- if (envnonempty('DEBCONF_DB_OVERRIDE')) {
- $ENV{DEBCONF_APT_PROGRESS_DB_OVERRIDE} =
- $ENV{DEBCONF_DB_OVERRIDE};
- }
-
- $ENV{DEBCONF_DB_REPLACE} = 'configdb';
- $ENV{DEBCONF_DB_OVERRIDE} = 'Pipe{infd:none outfd:none}';
-
- @ARGV = @_;
- }
-
- import Debconf::Client::ConfModule;
- }
-
- sub passthrough (@) {
- my $priority = Debconf::Client::ConfModule::get('debconf/priority');
-
- defined(my $pid = fork) or die "$0: can't fork: $!\n";
- if (!$pid) {
- close STATUS_READ;
- close COMMAND_WRITE;
- $^F = 6; # avoid close-on-exec
- checkdup2(0, 5);
- if (exists $ENV{DEBCONF_REDIR} and $ENV{DEBCONF_REDIR}) {
- checkdup2(3, 6);
- checkclose(3);
- } else {
- checkdup2(1, 6);
- }
- if (fileno(COMMAND_READ) != 0) {
- checkdup2(fileno(COMMAND_READ), 0);
- close COMMAND_READ;
- }
- if (fileno(APT_LOG) != 1) {
- checkclose(1);
- checkdup2(fileno(APT_LOG), 1);
- }
- if (fileno(APT_LOG) != 2) {
- checkclose(2);
- checkdup2(fileno(APT_LOG), 2);
- }
- close APT_LOG;
- delete $ENV{DEBIAN_HAS_FRONTEND};
- delete $ENV{DEBCONF_REDIR};
- delete $ENV{DEBCONF_SYSTEMRC};
- delete $ENV{DEBCONF_PIPE}; # just in case ...
- $ENV{DEBIAN_FRONTEND} = 'passthrough';
- $ENV{DEBIAN_PRIORITY} = $priority;
- $ENV{DEBCONF_READFD} = 5;
- $ENV{DEBCONF_WRITEFD} = 6;
- $ENV{APT_LISTCHANGES_FRONTEND} = 'none';
- exec @_;
- }
-
- close STATUS_WRITE;
- close COMMAND_READ;
- return $pid;
- }
-
- sub run_progress ($$@) {
- my $from = shift;
- my $to = shift;
- my $command = shift;
- local (*STATUS_READ, *STATUS_WRITE);
- local (*COMMAND_READ, *COMMAND_WRITE);
- local *APT_LOG;
-
- Debconf::Client::ConfModule::progress(
- 'INFO', 'debconf-apt-progress/preparing');
-
- reservefds(4, 5, 6);
-
- pipe STATUS_READ, STATUS_WRITE or die "$0: can't create status pipe: $!";
- checkdup2(fileno(STATUS_WRITE), 4);
- open STATUS_WRITE, '>&=4'
- or die "$0: can't reopen STATUS_WRITE as fd 4: $!";
- nocloexec(\*STATUS_WRITE);
- pipe COMMAND_READ, COMMAND_WRITE or die "$0: can't create command pipe: $!";
- nocloexec(\*COMMAND_READ);
- use IO::Handle;
- COMMAND_WRITE->autoflush(1);
-
- if (defined $logfile) {
- open APT_LOG, '>>', $logfile
- or die "$0: can't open $logfile: $!";
- } elsif ($logstderr) {
- open APT_LOG, '>&STDERR'
- or die "$0: can't duplicate stderr: $!";
- } else {
- open APT_LOG, '>', '/dev/null'
- or die "$0: can't open /dev/null: $!";
- }
- nocloexec(\*APT_LOG);
-
- my $pid = passthrough $command,
- '-o', 'APT::Status-Fd=4',
- '-o', 'APT::Keep-Fds::=5',
- '-o', 'APT::Keep-Fds::=6',
- @_;
-
- while (<STATUS_READ>) {
- chomp;
- my ($status, $pkg, $percent, $description) = split ':', $_, 4;
-
- my ($min, $len);
- if ($status eq 'dlstatus') {
- $min = 0;
- $len = 15;
- }
- elsif ($status eq 'pmstatus') {
- $min = 15;
- $len = 85;
- }
- elsif ($status eq 'media-change') {
- Debconf::Client::ConfModule::subst(
- 'debconf-apt-progress/media-change', 'MESSAGE',
- $description);
- my @ret = Debconf::Client::ConfModule::input(
- 'critical', 'debconf-apt-progress/media-change');
- $ret[0] == 0 or
- die "Can't display media change request!\n";
- Debconf::Client::ConfModule::go();
- print COMMAND_WRITE "\n" || die "can't talk to command fd: $!";
- next;
- }
- else {
- next;
- }
-
- $percent = ($percent * $len / 100 + $min);
- $percent = ($percent * ($to - $from) / 100 + $from);
- $percent =~ s/\..*//;
- Debconf::Client::ConfModule::progress('SET', $percent);
- Debconf::Client::ConfModule::subst(
- 'debconf-apt-progress/info', 'DESCRIPTION',
- $description);
- Debconf::Client::ConfModule::progress(
- 'INFO', 'debconf-apt-progress/info');
- }
-
- waitpid $pid, 0;
- my $status = $?;
-
- Debconf::Client::ConfModule::progress('SET', $to);
-
- return ($status >> 8);
- }
-
- sub start_bar ($$) {
- my ($from, $to) = @_;
- Debconf::Client::ConfModule::progress(
- 'START', $from, $to, 'debconf-apt-progress/title');
- Debconf::Client::ConfModule::progress(
- 'INFO', 'debconf-apt-progress/preparing');
- }
-
- sub stop_bar () {
- Debconf::Client::ConfModule::progress('STOP');
- Debconf::Client::ConfModule::stop();
- }
-
- if (envnonempty('DEBCONF_APT_PROGRESS_DB_REPLACE')) {
- $ENV{DEBCONF_DB_REPLACE} = $ENV{DEBCONF_APT_PROGRESS_DB_REPLACE};
- } else {
- delete $ENV{DEBCONF_DB_REPLACE};
- }
- if (envnonempty('DEBCONF_APT_PROGRESS_DB_OVERRIDE')) {
- $ENV{DEBCONF_DB_OVERRIDE} = $ENV{DEBCONF_APT_PROGRESS_DB_OVERRIDE};
- } else {
- delete $ENV{DEBCONF_DB_OVERRIDE};
- }
-
- my @saved_argv = @ARGV;
-
- my $result = GetOptions('config' => \$config,
- 'start' => \$start,
- 'from=i' => \$from,
- 'to=i' => \$to,
- 'stop' => \$stop,
- 'logfile=s' => \$logfile,
- 'logstderr' => \$logstderr);
-
- unless ($start) {
- if (defined $from and not defined $to) {
- die "$0: --from requires --to\n";
- } elsif (defined $to and not defined $from) {
- die "$0: --to requires --from\n";
- }
- }
-
- my $mutex = 0;
- ++$mutex if $config;
- ++$mutex if $start;
- ++$mutex if $stop;
- if ($mutex > 1) {
- die "$0: must use only one of --config, --start, or --stop\n";
- }
-
- if (($config or $stop) and (defined $from or defined $to)) {
- die "$0: cannot use --from or --to with --config or --stop\n";
- }
-
- start_debconf(@saved_argv) unless $config;
-
- my $status = 0;
-
- if ($config) {
- print <<'EOF';
- DEBCONF_APT_PROGRESS_DB_REPLACE="$DEBCONF_DB_REPLACE"
- DEBCONF_APT_PROGRESS_DB_OVERRIDE="$DEBCONF_DB_OVERRIDE"
- export DEBCONF_APT_PROGRESS_DB_REPLACE DEBCONF_APT_PROGRESS_DB_OVERRIDE
- DEBCONF_DB_REPLACE=configdb
- DEBCONF_DB_OVERRIDE='Pipe{infd:none outfd:none}'
- export DEBCONF_DB_REPLACE DEBCONF_DB_OVERRIDE
- EOF
- } elsif ($start) {
- $from = 0 unless defined $from;
- $to = 100 unless defined $to;
- start_bar($from, $to);
- } elsif (defined $from) {
- $status = run_progress($from, $to, @ARGV);
- } elsif ($stop) {
- stop_bar();
- } else {
- start_bar(0, 100);
- $status = run_progress(0, 100, @ARGV);
- stop_bar();
- }
-
- exit $status;
-
-